import sys, os
here=os.path.join(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0,here)
sys.path.insert(0,os.path.dirname(here))
import unittest
from epubbuilder.epub import EpubBook
from epubbuilder import ez_epub
import base64

#templates = os.path.join(here, 'templates')

def get_image(outfile):
    image="""iVBORw0KGgoAAAANSUhEUgAAAGkAAAAfAQMAAADeN3GlAAAAAXNSR0IArs4c6QAAAARnQU1BAACx
jwv8YQUAAAAGUExURQAAAP///6XZn90AAAAJcEhZcwAADsQAAA7EAZUrDhsAAABTSURBVCjPY/iP
DBoGD7cZSD///98eyl39/9//7///74dy36Nx+znsP3x8vBjGLXv3/3ve3e0wLrsdiLsexq2/B+L2
Y1d8vE8eZJTw4AoNYrn/GwDswpBvKPlXlAAAAABJRU5ErkJggg=="""

    with open(outfile,'wb') as f:
        f.write(base64.b64decode(image))
    return outfile

def get_file(outfile):
    if not os.path.exists(outfile):
        f=open(outfile,'w')
        f.close()
    return outfile

class TestEpub(unittest.TestCase):

    def setUp(self):

        testdir= os.path.join(sys.path[0],'out')
        if not os.path.exists(testdir):
            os.mkdir(testdir)

        self.testdir=testdir
        self.cover_file=get_image(os.path.join(testdir,'cover.png'))
        self.image_file=get_image(os.path.join(testdir,'image.png'))
        self.css_file=get_file(os.path.join(testdir,'main.css'))

    def test_make_book(self):
        """test to create a test epub file"""

        def getMinimalHtml(text):
            return """<!DOCTYPE html>
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head><title>%s</title></head>
 <body><p>%s</p></body>
 </html>
 """ % (text, text)

        book = EpubBook()
        book.setTitle('Most Wanted Tips for Aspiring Young Pirates')
        book.addCreator('Monkey D Luffy')
        book.addCreator('Guybrush Threepwood')
        book.addMeta('contributor', 'Smalltalk80', role = 'bkp')
        book.addMeta('date', '2010', event = 'publication')
        
        book.addTitlePage()
        book.addTocPage()

        book.addCover(self.cover_file)
        book.addCss(self.css_file, 'main.css')

        n1 = book.addHtml('', '1.html', getMinimalHtml('Chapter 1'))
        n11 = book.addHtml('', '2.html', getMinimalHtml('Section 1.1'))
        n111 = book.addHtml('', '3.html', getMinimalHtml('Subsection 1.1.1'))
        n12 = book.addHtml('', '4.html', getMinimalHtml('Section 1.2'))
        n2 = book.addHtml('', '5.html', getMinimalHtml('Chapter 2'))
        img = book.addImage(self.image_file, 'image.png')
        n21 = book.addHtmlForImage(img)

        book.addSpineItem(n1)
        book.addSpineItem(n11)
        book.addSpineItem(n111)
        book.addSpineItem(n12)
        book.addSpineItem(n2)
        book.addSpineItem(n21)

        # You can use both forms to add TOC map
        #t1 = book.addTocMapNode(n1.destPath, '1')
        #t11 = book.addTocMapNode(n11.destPath, '1.1', parent = t1)
        #t111 = book.addTocMapNode(n111.destPath, '1.1.1', parent = t11)
        #t12 = book.addTocMapNode(n12.destPath, '1.2', parent = t1)
        #t2 = book.addTocMapNode(n2.destPath, '2')
        
        book.addTocMapNode(n1.destPath, '1')
        book.addTocMapNode(n11.destPath, '1.1', 2)
        book.addTocMapNode(n111.destPath, '1.1.1', 3)
        book.addTocMapNode(n12.destPath, '1.2', 2)
        book.addTocMapNode(n2.destPath, '2')
        book.addTocMapNode(n21.destPath, '2.1',2)

        rootDir = os.path.join(self.testdir,r'test')
        book.createBook(rootDir)
        EpubBook.createArchive(rootDir, rootDir + '.epub')
        #EpubBook.checkEpub('epubcheck-1.0.5.jar', rootDir + '.epub')
        self.assertTrue(os.path.exists(rootDir+'.epub'))

class TestEZPub(unittest.TestCase):

    def setUp(self):
        testdir= os.path.join(sys.path[0],'out')
        if not os.path.exists(testdir):
            os.mkdir(testdir)

        self.testdir=testdir

    def test_ez_section(self):
        """test create epub using ez_pub"""
        
        book=ez_epub.Book()
        book.title = 'Most Wanted Tips for Aspiring Young Pirates'
        book.authors = ['Monkey D Luffy','Guybrush Threepwood']
        book.lang = 'en-US'
        sections=[]
        section=ez_epub.Section()
        section.title= 'Chapter 1'
        section.text = ['Chapter 1'] 
        sections.append(section)
        section=ez_epub.Section()
        section.title = 'Section 1.1'
        section.text= ['Section 1',[('Section 1.1',''),('Subsection 1.1.1','class_name')]]
        sections.append(section)
        book.sections=sections
        book.make(os.path.join(self.testdir,'test2'))
        self.assertTrue(os.path.exists(os.path.join(self.testdir,'test2.epub')))


if __name__ == '__main__':
    unittest.main()
